let feedbackData = [];
let currentPage = 1;
const itemsPerPage = 8; // 每页显示 8 项

function fetchFeedback() {
    fetch('/webapi/patient/advice')
        .then(response => response.json())
        .then(data => {
            feedbackData = data;
            renderFeedback();
        })
        .catch(error => console.error('Error fetching feedback:', error));
}

function renderFeedback() {
    const feedbackContainer = document.getElementById('feedbackContainer');
    feedbackContainer.innerHTML = ''; // 清空当前内容

    const startIndex = (currentPage - 1) * itemsPerPage;
    const endIndex = startIndex + itemsPerPage;
    const currentFeedbacks = feedbackData.slice(startIndex, endIndex);

    currentFeedbacks.forEach(feedback => {
        const feedbackDiv = document.createElement('div');
        feedbackDiv.className = 'feedback-item';
        feedbackDiv.innerHTML = `
            <span style="width: 250px;">${feedback.content}</span>
            <svg style="height: 30px; width: 30px; cursor: pointer;" onclick="confirmDelete(${feedback.id})" t="1730622131527" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="8584" width="200" height="200"><path d="M853.4 333.5H199.8c-3.9 0-7 3.4-6.6 7.3l60.1 630c0 29.3 23.8 53.1 53.1 53.1H747c29.3 0 53.1-23.8 53.1-53.1l60.1-630c0.2-3.9-2.8-7.3-6.8-7.3zM388.8 890.9h-19.5c-13 0-23.6-10.6-23.6-23.6l-24.3-376c0-13 10.6-23.6 23.6-23.6h19.5c13 0 23.6 10.6 23.6 23.6l24.3 376c0 13-10.6 23.6-23.6 23.6z m171.3-23.6c0 13-10.6 23.6-23.6 23.6H517c-13 0-23.6-10.6-23.6-23.6v-376c0-13 10.6-23.6 23.6-23.6h19.5c13 0 23.6 10.6 23.6 23.6v376z m148.3 0c0 13-10.6 23.6-23.6 23.6h-19.5c-13 0-23.6-10.6-23.6-23.6l24.3-376c0-13 10.6-23.6 23.6-23.6h19.5c13 0 23.6 10.6 23.6 23.6l-24.3 376zM869.3 153.2l-705 162c-4 0.9-7.9-1.6-8.8-5.5l-8.8-38.5c-6.8-29.5 11.8-59.2 41.3-66L800 64.5c29.5-6.8 59.2 11.8 66 41.3l8.8 38.5c0.9 4-1.5 8-5.5 8.9zM386.5 127.3c-6.5-27.9 11-56.1 39-62.6l98.2-22.8c27.9-6.5 56.1 11 62.6 39l3.8 16.2 44.3-10.3-3.1-13.3c-11.7-50.4-62.5-82-112.8-70.3L413.6 27.6c-50.4 11.7-82 62.5-70.3 112.8l3 13.1 43.9-10.2-3.7-16z" fill="#f3799e" p-id="8585"></path></svg>
        `;
        feedbackContainer.appendChild(feedbackDiv);
    });

    document.getElementById('pageInfo').textContent = `第 ${currentPage} 页`;
    document.getElementById('prevBtn').disabled = currentPage === 1;
    document.getElementById('nextBtn').disabled = endIndex >= feedbackData.length;
}

function changePage(direction) {
    if (direction === 1) {
        currentPage++;
    } else if (direction === -1) {
        currentPage--;
    }
    renderFeedback();
}

function confirmDelete(id) {
    const isConfirmed = window.confirm('确认删除该反馈建议吗？');
    if (isConfirmed) {
        deleteFeedback(id);
    }
}

function deleteFeedback(id) {
    fetch(`/api/feedback/${id}`, {
        method: 'DELETE',
    })
        .then(() => {
            feedbackData = feedbackData.filter(feedback => feedback.id !== id);
            renderFeedback();
        })
        .catch(error => console.error('Error deleting feedback:', error));
}

function addFeedback() {
    const newFeedbackContent = document.getElementById('newFeedback').value;
    if (newFeedbackContent.trim()) {
        const newFeedback = { content: newFeedbackContent };

        fetch('/api/feedback', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(newFeedback),
        })
            .then(() => {
                fetchFeedback(); // 重新获取反馈列表
                hideFeedbackForm();
            })
            .catch(error => console.error('Error adding feedback:', error));
    } else {
        alert('请输入反馈建议！');
    }
}
function showFeedbackForm() {
    document.getElementById('feedbackForm').style.display = 'block';
}

function hideFeedbackForm() {
    document.getElementById('feedbackForm').style.display = 'none';
    document.getElementById('newFeedback').value = ''; // 清空文本框
}
// 初始获取反馈数据
fetchFeedback();
